package com.bsj.pay.service.impl;

import com.bsj.pay.config.GlobalConfig;
import com.bsj.pay.dao.AccountDao;
import com.bsj.pay.dao.GoodsDao;
import com.bsj.pay.dao.PayPoolDao;
import com.bsj.pay.model.AccountBean;
import com.bsj.pay.model.GoodsBean;
import com.bsj.pay.model.PayGoodsBean;
import com.bsj.pay.model.PayPoolBean;
import com.bsj.pay.service.PayPoolService;
import com.bsj.pay.util.SpringUtil;
import com.bsj.pay.util.platform.PayInterface;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@Service
public class PayPoolServiceImpl extends BaseServiceImpl<PayPoolDao, PayPoolBean> implements PayPoolService {

    @Resource
    private GoodsDao goodsDao;

    @Resource
    private AccountDao accountDao;

    @Transactional
    @Override
    public Map<String, String> getOneCanUsePayUrl(Integer amount, String pay_type) {
        PayPoolBean result = dao.getOneCanUsePayUrl(amount, pay_type);
        if (result == null) {
            return null;
        }
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("payUrl", result.getPay_url());
        resultMap.put("goodsId", result.getGoods_id().toString());
        resultMap.put("cookies", result.getCookies());
        resultMap.put("orderNo", result.getOrder_no());

        PayPoolBean updateBean = new PayPoolBean();
        updateBean.setId(result.getId());
        updateBean.setStatus(0);
        dao.update(updateBean);
        return resultMap;
    }

    @Override
    public void generatePayUrl(PayGoodsBean payGoods) {
        String operator = "weidian";
        GoodsBean goods = goodsDao.getOneCanDispatchGoods(payGoods.getAmount(), payGoods.getPay_type(), 0);
        if (goods == null) {
            logger.error("暂无金额为{}的商品!", payGoods.getAmount());
            return;
        }
        AccountBean account = accountDao.getOneCanDispatchOrderByAuthType(operator, GlobalConfig.order_account_type);
        if (account == null) {
            logger.error("暂无购买账号，请检查账户配置!");
            return;
        }

        try {
            PayInterface inf = (PayInterface) SpringUtil.getBean(operator + "Util");
            Map<String, String> resultMap = inf.getPayUrl(goods, payGoods.getPay_type(), account);

            PayPoolBean payPool = new PayPoolBean();
            payPool.setExpire_time(System.currentTimeMillis() / 1000 + 60 * 30); // 30分钟失效
            payPool.setStatus(1);
            payPool.setAmount(payGoods.getAmount());
            payPool.setGoods_id(goods.getId());
            payPool.setPay_goods_id(payGoods.getId());
            payPool.setPay_type(payGoods.getPay_type());
            payPool.setPay_url(resultMap.get("payUrl"));
            payPool.setCookies(resultMap.get("cookies"));
            payPool.setNotify_token(resultMap.get("notifyToken"));
            payPool.setOrder_no(resultMap.get("orderNo"));
            dao.insert(payPool);
        } catch (Exception e) {
            // 当account失效时执行
            if (e.getMessage().contains("LOGIN ERROR")) {
                AccountBean updateAccount = new AccountBean();
                updateAccount.setId(account.getId());
                updateAccount.setLogin_status(2);
                accountDao.update(updateAccount);
            }
            logger.error(e.getMessage());
        }
    }

    @Override
    public void removeExpired() {
        dao.removeExpired();
    }
}
